package com.taobao.ic.ticket.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class FileUtils {
	private final static long K_BYTES = 1024;
	private final static long M_BYTES = 1024 * 1204;
	private final static long G_BYTES = 1024 * 1204 * 1024;
	private final static int length = 1024 * 1204;// 一次读取多大的文件

	public FileUtils() {
	}

	public static void WriteTextToFile(String path, String text) {
		FileWriter frwriter = null;

		try {
			frwriter = new FileWriter(new File(path));
			frwriter.write(text);
			System.out.println("写文件成功");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				frwriter.flush();
				frwriter.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public static String getTextFromFile(String path) {
		BufferedReader freader = null;
		StringBuffer text = new StringBuffer();
		// 读文件
		int count = 1;
		try {
			freader = new BufferedReader(new FileReader(new File(path)));
			String text1 = "";
			while ((text1 = freader.readLine()) != null) {
				text.append(count + "," + text1 + "\n");
				count++;
			}
			System.out.println("替换操作成功，开始保存文件");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				if (freader != null) {
					freader.close();
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		return text.toString();
	}

	/*
	 * 文件的切割 String path 切割文件的路径 size 　　　　 子文件的大小
	 */
	public static void filesplit(String path, long size) throws Exception {
		if (path == null)
			throw new Exception("源文件不能为空...");
		File file = new File(path);
		if (!file.exists())
			throw new Exception("源文件不存在...");
		long num = file.length() % size == 0 ? file.length() / size : file
				.length() / size + 1;
		String list[] = new String[(int) num];// 用于存放分割后的结果
		@SuppressWarnings("resource")
		FileInputStream reader = new FileInputStream(file);
		long beginIndex = 0, endIndex = 0;
		int readcount = 0;
		byte buffer[] = new byte[length];
		for (int i = 0; i < num; i++) {
			list[i] = file.getAbsolutePath() + ".depart" + i + ".temp";
			FileOutputStream writer = new FileOutputStream(list[i]);
			endIndex = (endIndex + size) > file.length() ? file.length()
					: endIndex + size;
			for (; beginIndex < endIndex;) {
				if (endIndex - beginIndex >= length) {
					readcount = reader.read(buffer);
					beginIndex += readcount;
					writer.write(buffer);
				} else {
					// 下面的就不能直接读取1024个字节了,就要一个一个字节的读取了
					for (; beginIndex < endIndex; beginIndex++) {
						writer.write(reader.read());
					}
					continue;
				}
			}
			writer.close();

		}
	}

	/**
	 * 把当前文件夹下的文件，合并为一个文件
	 * 
	 * @param path
	 * @param newString
	 * @throws Exception
	 */
	public static void union(String path, String newString) throws Exception {
		File file = new File("F:\\mac");
		List<String> listsFileName = new ArrayList<String>();
		for(File fi : file.listFiles()){
			listsFileName.add(fi.getAbsolutePath());
		}
		Collections.sort(listsFileName);
		File newFile = new File(newString);
		byte buffer[] = new byte[length];
		if (!newFile.getParentFile().exists())
			throw new Exception("你合并的文件夹的不存在...");
		FileOutputStream writer = new FileOutputStream(newString);
		for (String f : listsFileName) {
			FileInputStream reader = new FileInputStream(f);
			while ((reader.read(buffer)) != -1) {
				writer.write(buffer);
			}
			reader.close();
		}
		writer.close();
	}

	public static void main(String[] args) throws Exception {
	}
}
